/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xdxn.editor.studio.biz.barrage;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import xdxn.editor.studio.data.ProjectManager;
import xdxn.editor.studio.data.barrage.Muzzle;
import xdxn.editor.studio.utils.IDFactory;

/**
 *
 * @author Cai Han
 */
public class MuzzleTableModel extends AbstractTableModel {

    private final IDFactory _idFactory;
    private ArrayList<Muzzle> _list;
    private final String[] _columnNames = { "id" , "描述" };

    public MuzzleTableModel( ArrayList<Muzzle> list ) {
        this._list = list;
        _idFactory = new IDFactory();
    }

    @Override
    public int getRowCount() {
        return this._list == null ? 0 : this._list.size();
    }

    @Override
    public int getColumnCount() {
        return this._columnNames.length;
    }

    @Override
    public void setValueAt( Object obj , int rowIndex , int columnIndex ) {
        Muzzle muzzle = this._list.get(rowIndex);
        if ( this._columnNames[columnIndex].equals("描述") ) {
            muzzle.name = obj.toString();
        }
        super.setValueAt(obj , rowIndex , columnIndex);
    }

    @Override
    public Object getValueAt( int rowIndex , int columnIndex ) {
        if ( this._list == null ) {
            return null;
        }
        Muzzle muzzle = this._list.get(rowIndex);
        switch ( this._columnNames[columnIndex] ) {
            case "描述":
                return muzzle.name;
            case "id":
                return muzzle.id;
        }
        return null;
    }

    @Override
    public String getColumnName( int i ) {
        return this._columnNames[i];
    }

    @Override
    public boolean isCellEditable( int row , int column ) {
        return !(column == 0);
    }

    public void setList( ArrayList<Muzzle> list ) {
        this._list = list;
        this.fireTableStructureChanged();
    }

    public Muzzle add( int rowIndex ) {
        //
        if ( this._list == null ) {
            return null;
        }
        //
        Muzzle muzzle = new Muzzle();
        muzzle.id = this._idFactory.requestID();
        muzzle.name = "炮口" + muzzle.id;
        //
        int size = this._list.size();
        if ( rowIndex < 0 || rowIndex > size - 1 ) {
            this._list.add(muzzle);
            fireTableRowsInserted(size , size);
        } else {
            this._list.add(rowIndex , muzzle);
            fireTableRowsInserted(rowIndex , rowIndex);
        }
        // 设置当前炮口
        ProjectManager.getInstance().current().current_barrage.current_muzzle = muzzle;

        return muzzle;
    }

    public boolean update( int rowIndex , Muzzle muzzle ) {
        if ( this._list == null || this._list.isEmpty() || rowIndex < 0 || rowIndex > this._list.size() - 1 ) {
            return false;
        }

        this._list.set(rowIndex , muzzle);
        fireTableRowsUpdated(rowIndex , rowIndex);

        return true;
    }

    public boolean remove( int rowIndex ) {
        if ( this._list == null || this._list.isEmpty() || rowIndex < 0 || rowIndex > this._list.size() - 1 ) {
            return false;
        }
        _list.remove(rowIndex);
        this.fireTableRowsDeleted(rowIndex , rowIndex);
        return true;
    }

    

}
